<template>
  <div class="RefundDialog">
    <object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0> 
      <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
    </object>
    <el-dialog title="打印发票" :visible.sync="$parent.PrintInvoiceFlag" width="30%" v-dialogDrag border @close="closeDialog()">
      <el-form :model="printData"   :inline="true" label-width="120px" ref="Refund" :rules="redRules"  :disabled="printFlag">
        <el-row :gutter="5">
          <el-form-item label="发票代码"  prop="invoiceItem.invoiceCode">
            <el-input v-model="printData.invoiceItem.invoiceCode" :maxlength="20"></el-input>
          </el-form-item>
          <el-form-item label="发票号:" prop="invoiceItem.invoiceNo">
            <el-input v-model="printData.invoiceItem.invoiceNo" :maxlength="20"></el-input>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="发票抬头"  prop="invoiceItem.invoiceTitle">
            <el-input v-model="printData.invoiceItem.invoiceTitle"  :maxlength="100" class="w425"></el-input>
          </el-form-item>

        </el-row>
        <el-row :gutter="5">
          <el-form-item label="发票金额:"  >
            <el-input v-model="changeCharge" :disabled="true" :maxlength="20"></el-input>
          </el-form-item>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary"  @click="PrintDataMethod()">打印发票</el-button>

      </div>
    </el-dialog>
  </div>
</template>
<script>
  import * as Type from '@/api/service'
  import {getLodop} from '@/utils/lodopFuncs'
    export default {
        name: "PrintInvoiceDialog",
      props: {
        PrintInvoiceFlag: {
          type: Boolean
        },
        multipleSelectionHistory: {
          type: Array,
          require: true
        },//选择结算记录
        businessCode: {
          type: String,
          default: ""
        }
      },
      data() {
        return {
          printData:{
            'settleIds':[],//结算主键集合
            'invoiceItem':{
              'invoiceCode':'',//发票代码
              'invoiceNo':'',//发票号码
              'invoiceTitle':'',//发票抬头
              'invoiceCharge':'',//发票金额
              'remark':''//备注
            },
            'businessCode':this.businessCode
          },
          printFlag:false //打印标志
          , redRules: {
            'invoiceItem.invoiceCode': [
              {required: true, message: '请输入发票代码', trigger: 'blur'},
              {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
            ],
            'invoiceItem.invoiceNo': [
              {required: true, message: '请输入发票号', trigger: 'blur'},
              {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
            ], 'invoiceItem.invoiceTitle': [
              {required: true, message: '请输入发票抬头', trigger: 'change'},
              {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
            ],
          },
        }
      },
      created() {
        // this.loadChargeInvoiceAllInfo()
      },
     computed:{
          //计算发票金额
          changeCharge(){
            //定义总合计
            let settleTotal=0;
            let selectData=this.multipleSelectionHistory
            if(selectData && selectData.length>0) {
              let selectItem = null
              let settleCharge = 0;
              for (let i = 0; i < selectData.length; i++) {
                selectItem = selectData[i]
                //转换为数值运算
                settleCharge = new Number(selectItem.settleCharge)
                settleTotal += settleCharge

              }   //返回合计金额
              settleTotal= Number(settleTotal).toFixed(2)
              return settleTotal;
            }
        }
      },
      watch: {
        '$parent.PrintInvoiceFlag'() {
          if (this.$parent.PrintInvoiceFlag == false) {
           //清空所有预加载项
          }
        }
      },
      methods: {
        /*打印*/
        PrintDataMethod:function(){
           let this_print=this;
          //业务编码非空
          if(this.businessCode!=null && this.businessCode!=""){
            this.printData.businessCode=this.businessCode
            let selectData=this.multipleSelectionHistory
            this.printData.settleIds=[]
            //计算结果赋值给发票金额
            this.printData.invoiceItem.invoiceCharge=this.changeCharge;
            if(selectData && selectData.length>0){
               let selectItem=null
               let idValue=null;
               let versionValue=null;
               for(let i=0;i<selectData.length;i++){
                 selectItem=selectData[i]
                 idValue=selectItem.id
                 versionValue=selectItem.version
                 this.printData.settleIds.push({
                   id:idValue,
                   version:versionValue
                 })
               }
            }
            //定义发票打印数据
            let postData = this_print.printData
            //打印发票接口
            Type['saveChargSettleToInvoice'](postData).then(response => {
              let result = response;
              if (result && result.data){
                this_print.PrintInvoice(result.data);
              }

            }).catch(error => {
              this_print.$message(error)
            })
          }
        }, closeDialog() {
          this.$parent.PrintInvoiceFlag = false
        },//重打发票
        PrintInvoice(printData){
          let this_print=this;
          let LODOP =  getLodop();
          if (LODOP == null || !LODOP) {
            this_print.$.messager.alert('系统提示', '您的浏览器不支持打印控件，请用IE重试或安装打印控件后重试');
            return;
          }
          //业务编码
          let businessCode=printData.businessCode;
          //交款人
          let persion="交款人";
          //项目名称
          let itemName="项目名称";
          let itemPrice="XXXX";
          let itemTotal="XXXX";
          let bigTitle="壹贰叁肆伍陆柒捌玖";
          //收费日期
          let  businessTime="2020   05   22"
          //业务办理人
          let  businessPersion="管理员";
          //设置纸张
          //LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");
          //设置字体
          LODOP.SET_PRINT_STYLE("FontSize",11);
          //调用打印控件进行测试

          LODOP.ADD_PRINT_TEXT(39,50,150,50, businessCode);
          LODOP.ADD_PRINT_TEXT(39,250,150,50, businessTime);
          LODOP.ADD_PRINT_TEXT(60,50,150,50, persion);
          LODOP.ADD_PRINT_TEXT(80,50,150,50, itemName);
          LODOP.ADD_PRINT_TEXT(80,120,150,50, itemPrice);
          LODOP.ADD_PRINT_TEXT(300,220,150,50, itemTotal);
          LODOP.ADD_PRINT_TEXT(300,420,150,50, bigTitle);
          LODOP.ADD_PRINT_TEXT(340,420,150,50, businessPersion);
          //打印预览
          LODOP.PREVIEW();
          //直接打印
          //LODOP.PRINT();
          //LODOP.PRINT_DESIGN();
          //关闭打印窗口
          this_print.printData={
            'settleIds':[],//结算主键集合
            'invoiceItem':{
              'invoiceCode':'',//发票代码
              'invoiceNo':'',//发票号码
              'invoiceTitle':'',//发票抬头
              'invoiceCharge':'',//发票金额
              'remark':''//备注
            },
            'businessCode':this_print.businessCode
          }
          this_print.$parent.PrintInvoiceFlag = false
          //查询一下
        }




       }
    }
</script>

<style scoped>
  .w425 {
    width: 25.6vw;
    min-width: 40px;
  }
</style>
